import store from '@/store/index.js';
import {
  checkType,
  isEmpty
} from "@/common/util.js"
const configObj = {
  // 本地开发  只需要修改develop
  develop: {
    // 开发环境使用项目本地图片资源，发布正式版或体验版小程序务必将图片资源上传到cdn服务器，并同时删除本地static/images文件夹后，再将代码打包编译后发布上传
    cdnUrl: '/static/images/',
    ipayMobileView: 'https://ipay-test.nbcb.com.cn/ipayMobileView5/api',
    // iassbff: "https://ipay-test.nbcb.com.cn/iass-bff/dev",
    iassbff: "https://ipay-test.nbcb.com.cn/iass-bff/sit",
    // iassbff: "https://ipay-test.nbcb.com.cn/iass-bff/uat",
    // iassbff: "https://ipay.nbcb.com.cn/iass-bff"
  },
  // 体验版本  只需要修改trial
  trial: {
    cdnUrl: '/static/images/',
    ipayMobileView: 'https://ipay-test.nbcb.com.cn/ipayMobileView3/api',
    // iassbff: "https://ipay-test.nbcb.com.cn/iass-bff/uat",
    iassbff: "https://ipay-test.nbcb.com.cn/iass-bff/sit",
    // iassbff: "https://ipay.nbcb.com.cn/iass-bff"
  },
  // 正式生产 只需要修改release product
  release: {
    cdnUrl: '/static/images/',
    ipayMobileView: 'https://aapw.nbcb.com.cn/ipayMobileView/api',
    iassbff: "https://ipay.nbcb.com.cn/iass-bff"
  }
}
// 小程序配置信息 
export function getConfigInfo() {
  try {
    const info = uni.getAccountInfoSync();
    let res = info.miniProgram,
      configData = configObj[res.envVersion] || configObj.release;
    Object.keys(configData).forEach(key => {
      res[key] = configData[key]
    })
    return res
  } catch (e) {
    return {
      ...configObj.release,
      appId: "wxaad37e863eeaa43c", // wxaad37e863eeaa43c
    };
  }
}
// 处理图片静态资源，拼接cdn链接
export function getImg(url = '', defaultImg = '') {
  const urlIsStr = checkType(url) == 'string',
    defaultImgIsStr = checkType(defaultImg) == 'string';
  let cdnUrl = '/static/images/',
    res = '';
  if (store.getters && store.getters.configInfo && store.getters.configInfo.cdnUrl) {
    cdnUrl = store.getters.configInfo.cdnUrl
  }
  if (urlIsStr && !isEmpty(url)) {
    res = url.indexOf('http') >= 0 ? url : `${cdnUrl}${url}`
  }
  if (isEmpty(url) && defaultImgIsStr && !isEmpty(defaultImg)) {
    res = defaultImg.indexOf('http') >= 0 ? defaultImg : `${cdnUrl}${defaultImg}`
  }
  if (isEmpty(url) && isEmpty(defaultImg)) {
    res = `${cdnUrl}iassCashier/nb_logo.png`
  }
  return res
}